<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login</title>

<script type="text/javascript">
		var sendReq = getXmlHttpRequestObject();
		var receiveReq = getXmlHttpRequestObject()
		var mTimer;

		//Gets the browser specific XmlHttpRequest Object
		function getXmlHttpRequestObject() {
			if (window.XMLHttpRequest) {
				return new XMLHttpRequest();
			} else if(window.ActiveXObject) {
				return new ActiveXObject("Microsoft.XMLHTTP");
			} else {
				document.getElementById('p_status').innerHTML = 'Status: Cound not create XmlHttpRequest Object.  Consider upgrading your browser.';
			}
		}
		
		function sendLogoutRequest() {
			if (sendReq.readyState == 4 || sendReq.readyState == 0) {
				sendReq.open("POST", '../library/game_room.php', true);
				sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
				sendReq.onreadystatechange = sendLogoutRequestCallBack; 
				var param = 'logout=1';
				sendReq.send(param);
			}				
		}
		
		function sendLogoutRequestCallBack() {
			if (sendReq.readyState == 4) {
				window.location = "login.html";
			}
		}
				
		function sendReadyRequest() {
			if (sendReq.readyState == 4 || sendReq.readyState == 0) {
				sendReq.open("POST", '../library/game_room.php', true);
				sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
				sendReq.onreadystatechange = sendReadyRequestCallBack; 
				var param = 'ready=1';
				sendReq.send(param);
			}
			clearInterval(mTimer);
		}
		
		function sendReadyRequestCallBack() {
			if (sendReq.readyState == 4) {
				var response = eval("(" + sendReq.responseText + ")");
				document.getElementById('room_status').innerHTML = response.status;
			}
			mTimer = setTimeout('sendGetRoomReadyStatusRequest()', 2000); 
		}
		
		function sendNotReadyRequest() {
			if (sendReq.readyState == 4 || sendReq.readyState == 0) {
				sendReq.open("POST", '../library/game_room.php', true);
				sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
				sendReq.onreadystatechange = sendNotReadyRequestCallBack; 
				var param = 'ready=0';
				sendReq.send(param);
			}				
			clearInterval(mTimer);
		}
		
		function sendNotReadyRequestCallBack() {
			if (sendReq.readyState == 4) {
				var response = eval("(" + sendReq.responseText + ")");
				document.getElementById('room_status').innerHTML = response.status;
			}
			mTimer = setTimeout('sendGetRoomReadyStatusRequest()', 2000); 
		}
		
		function sendGetRoomReadyStatusRequest() {
			if (sendReq.readyState == 4 || sendReq.readyState == 0) {
				sendReq.open("POST", '../library/game_room.php', true);
				sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
				sendReq.onreadystatechange = sendGetRoomReadyStatusRequestCallBack; 
				var param = 'get_room_ready_status=1';
				sendReq.send(param);
			}
			clearInterval(mTimer);
		}
		
		function sendGetRoomReadyStatusRequestCallBack() {
			if (sendReq.readyState == 4) {
				var response = eval("(" + sendReq.responseText + ")");
					
				var div = document.getElementById('player_in_room_div');
				div.innerHTML = "";
				for (var item in response) {
					if (response[item] == "yes") { // Skip the "all_ready" status message
						continue;
					}
					var isReady;
					if (response[item] == 1 || response.all_ready == "yes") {
						isReady = "Ready";
					} else {
						isReady = "Not Ready";
					}
					
					div.innerHTML += "<tr ><td width=120>"+ item +"</td>" + 
					"<td width=100>"+ isReady +"</td></tr>";
				}
				
				if (response.all_ready) {
					window.location = "../controllers/ingame.php?view=map_view.php";
					return;
				}
			}
			mTimer = setTimeout('sendGetRoomReadyStatusRequest()', 2000); 
		}
		
		function sendAddAiPlayerRequest(ai) {
			if (sendReq.readyState == 4 || sendReq.readyState == 0) {
				sendReq.open("POST", '../library/game_room.php', true);
				sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
				sendReq.onreadystatechange = sendAddAiPlayerRequestCallBack; 
				var param = 'add_ai_player=' + ai;
				sendReq.send(param);
			}
			clearInterval(mTimer);
		}
		
		function sendAddAiPlayerRequestCallBack() {
			mTimer = setTimeout('sendGetRoomReadyStatusRequest()', 2000);
		}
		
	</script>

</head>
<body onload = "javacript:sendGetRoomReadyStatusRequest()">
<h1>Waiting Room</h1>
<h4><div id="room_status"></div></h4>
<table border="1" id = "player_status" >
	<tr>
		<td width="120"><h3>Player</h3></td>
		<td width="100"><h3>Status</h3></td>
	</tr>
	<tr>
		<td colspan="2"> 
			<div id = "player_in_room_div"> </div>
		</td>
	</tr>
	<tr>
		<td colspan="2">
			<input type="submit" name="ready_btn" id="ready_btn" value="Ready" 
			onclick="javascript:sendReadyRequest();" />
			<input type="submit" name="not_ready_btn" id="not_ready_btn" value="Not Ready" 
			onclick="javascript:sendNotReadyRequest();" />
		</td>
	</tr>

	
</table>
<br />
<a href="../chatroom/chat.html"> Chat </a>  |
<a href="#" onclick="javascipt:sendAddAiPlayerRequest(1)"> Add an Random AI player </a> |
<a href="#" onclick="javascipt:sendAddAiPlayerRequest(2)"> Add an Fight weakest AI player </a> |
<a href="#" onclick="javascipt:sendAddAiPlayerRequest(3)"> Add an Fight common AI player </a> |
<a href="#" onclick="javascript:sendLogoutRequest();"> Logout </a>

</body>
</html>
